package com.card.mail;

import java.util.Date;

import com.xiaoao.net.db.DBManager;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.utils.PrimaryKey;
import com.xiaoao.net.utils.PubFunc;
import com.xiaoao.net.utils.TimeUtils;

public class GlobMail{
	//邮件属性值
	public static final String MAILID = "mailId";
	public static final String ADDRESSOR = "addressor";
	public static final String TITLE = "title";
	public static final String DESC = "desc";
	public static final String REWARDS = "rewards";
	public static final String STARTDATE = "startDate";
	public static final String ENDDATE = "endDate";
	
	private String mailId;
	private String addressor;
	private String title;
	private String desc;
	private transient String rewardStr = "";
	private Date startDate;
	private Date endDate;
	
//	private transient List<Item> rewards = new ArrayList<>();
	
	public GlobMail(String addressor, String title, String desc, String rewardStr,
			Date startDate, Date endDate) {
		this.mailId = PrimaryKey.getPrimaryKey("c5Mail");
		this.addressor = addressor;
		this.title = title;
		this.desc = desc;
		this.rewardStr = rewardStr;
		this.startDate = startDate;
		this.endDate = endDate;

		initRewardList(rewardStr);
	}
	
	public GlobMail(Entity entity){
		this.mailId = getStringProperty(MAILID, entity);
		this.addressor = getStringProperty(ADDRESSOR, entity);
		this.title = getStringProperty(TITLE, entity);
		this.desc = getStringProperty(DESC, entity);
//		this.rewards = new Gson().fromJson(getStringProperty(REWARDS, entity), new TypeToken<List<Item>>(){}.getType());
		this.rewardStr = getStringProperty(REWARDS, entity);
		this.startDate = new Date(getLongProperty(STARTDATE, entity));
		this.endDate = new Date(getLongProperty(ENDDATE, entity));
		
		initRewardList(rewardStr);
	}
	/**
	 * 初始化奖励数据结构
	 * @param rewardstr
	 */
	public void initRewardList(String rewardstr){
//		rewards = ItemUtils.str2List(rewardstr, ",", ":");
//		if(rewards == null)
//			rewards = new ArrayList<>();
	}
	
	public String getStringProperty(String name, Entity entity) {
		return PubFunc.toString(entity.getProperty(name));
	}
	
	public long getLongProperty(String name, Entity entity) {
		return (long)entity.getProperty(name);
	}
	
	public void updateMailId() {
		this.mailId = PrimaryKey.getPrimaryKey("c5Mail");
	}

	public void setAddressor(String addressor) {
		this.addressor = addressor;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

//	public void setRewards(List<Item> rewards) {
//		this.rewards = rewards;
//	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}



	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
	public void saveDb() {
//		Gson gson = new GsonBuilder()
//				.setDateFormat(GSONUtils.DATEFORMAT)
//				.disableHtmlEscaping()
//				.create();
		
		Entity entity = new Entity(MailManager.T_MAIL_DATA,mailId);
		entity.setProperty(GlobMail.MAILID, mailId);
		entity.setProperty(GlobMail.ADDRESSOR, addressor);
		entity.setProperty(GlobMail.TITLE, title);
		entity.setProperty(GlobMail.DESC, desc);
//		entity.setProperty(GlobMail.REWARDS, gson.toJson(rewards));
		entity.setProperty(GlobMail.REWARDS, rewardStr);
		entity.setProperty(GlobMail.STARTDATE, startDate.getTime());
		entity.setProperty(GlobMail.ENDDATE, endDate.getTime());
		
		DBManager.getInstance().saveOrUpdate(entity);
	}


	/**
	 * 判断是否符合发送条件
	 * @return
	 */
	public boolean isFit() {
		Date now = new Date();
		return now.after(this.startDate) && now.before(this.endDate);
	}
	
//	public void giveItems(UserBase userBase){
//		if(rewards == null){
//			return;
//		}
//		for(Item item : rewards){
//			item.rewardItem(userBase);
//		}
//	}
	
	public String getStartDate() {
		return TimeUtils.ToString(startDate);
	}
	
	public String getEndDate() {
		return TimeUtils.ToString(endDate);
	}
	
	public String getMailId() {
		return mailId;
	}
	
	public  String getTitle() {
		return title;
	}

	public String getAddressor() {
		return addressor;
	}

	public String getDesc() {
		return desc;
	}
	
//	public List<Item> getRewards() {
//		return rewards;
//	}
	
	public String getRewardStr() {
		return rewardStr;
	}

}
